<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #wrap {
            position: relative;
        }
        .box {
            position: relative;
            width: 400px;
            height: 400px;
        }
        .box img {
            width: 400px;
        }
        .box span {
            position: absolute;
            display: none;
            width: 200px;
            height: 200px;
            background-color: rgba(255,15,0, 0.3);
            cursor: move;
        }
        .bigBox {
            display: none;
            position: absolute;
            left: 410px;
            top: 0;
            width: 400px;
            height: 400px;
            overflow: hidden;
        }
        .bigBox img {
            position: absolute;
        }
    </style>
    <script>
        class zoom{
            constructor (selector) {
                this.wrap = document.querySelector(selector)
                this.box = this.wrap.querySelector('.box')
                this.span = this.box.querySelector('span')
                this.bigBox = this.wrap.querySelector('.bigBox')
                this.img = this.bigBox.querySelector('img')
                this.bindEvents()
            }
            bindEvents(){
                this.box.onmouseenter = () => {
                    this.toggleDisplay('block');
                }
                this.box.onmouseleave = () => {
                    this.toggleDisplay('none');
                }
                this.box.onmousemove = (e) => {
                    // span的实际高度 = 鼠标到浏览器的高度 - 最外层盒子到浏览器的高度 - span高度的1/2
                    let top = e.clientY - this.box.offsetTop -this.span.offsetHeight/2,
                        left = e.clientX - this.box.offsetLeft -this.span.offsetWidth/2;
                    this.move(top,left);
                    
                }
            }
            toggleDisplay (display) {
                this.span.style.display = display
                this.bigBox.style.display = display
            }
            move(top,left){
                if(left < 0) left = 0;
                if (left > this.box.clientWidth - this.span.offsetWidth) {
                    left = this.box.clientWidth - this.span.offsetWidth 
                }
                if (top < 0) top = 0
                if (top > this.box.clientHeight - this.span.offsetHeight) {
                    top = this.box.clientHeight - this.span.offsetHeight
                }
                this.span.style.left = left +'px';
                this.span.style.top = top + 'px';
                this.img.style.left = -2 * left + 'px';
                this.img.style.top = -2 * top + 'px';
            }
        }
        window.onload = function(){
            new zoom("#wrap");
        }
    </script>
</head>
<body>
    <div id="wrap">
        <div class="box">
            <img src="./1.jpg" alt="">
            <span></span>
        </div>
        <div class="bigBox">
            <img src="./1.jpg" alt="">
        </div>
    </div>
</body>
</html>